﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TJDevHouse.Starcraft.Game;

namespace TJDevHouse.Starcraft.AIModuleConnector.ImportExport
{
	public class RawForcesDataImporter
	{
		public List<Force> ParseRawForcesData(string data)
		{
			//format:
			//	Forces:force1_name;p1_id|p1_name|p1_type|p1_race_id|p1_ally_enemy_neutral;p2_id|p2_name|p2_type|p2_race_id|p2_ally_enemy_neutral:force2_name.... etc.
			//
			//p1_ally_enemy_neutral value:
			//1 = ally
			//2 = enemy
			//3 = neutral

			string[] forcesData = data.Split(':');

			List<Force> forces = new List<Force>();

			bool isFirst = true;
			foreach (string forcesString in forcesData)
			{
				//skip the "Forces" prefix
				if (isFirst)
				{
					isFirst = false;
					continue;
				}

				

				string[] playersData = forcesString.Split(';');

				//get the force name
				Force force = new Force(playersData[0]);

				bool isFirstPlayer = true;
				foreach (string playerDataString in playersData)
				{
					//first string is the force name
					if (isFirstPlayer)
					{
						isFirstPlayer = false;
						continue;
					}

					string[] pDataParts = playerDataString.Split('|');

					int id = int.Parse(pDataParts[0]);
					string name = pDataParts[1];
					int raceID = int.Parse(pDataParts[3]);
					Game.RaceTypes.Race race = Races.GetRaceByBWAPIRaceID(raceID);

					int allyState = int.Parse(pDataParts[4]);

					bool isAlly = false;
					bool isEnemy = false;
					bool isNeutral = false;

					if (1 == allyState)
					{
						isAlly = true;
					}
					else if (2 == allyState)
					{
						isEnemy = true;
					}
					else
					{
						isNeutral = true;
					}

					Player player = new Player(id, name);

					player.IsAlly = isAlly;
					player.IsEnemy = isEnemy;
					player.IsNeutral = isNeutral;

					force.AddPlayer(player);
				}

				forces.Add(force);
			}

			return forces;
		}
	}
}
